<?php

/*
 * Copyright by Sygma Media Inovasi Ω2013
 * 
 * Lead Programmer : Sony K Wibisono,S.Si,M.T.I
 * E-mail : sony@sonywibisono.com
 * 
 *  
 * 
 */
global $is_login;


function _add(){
    $select_groups=  select_groups('0');
    echo "<h2>Add New User</h2>
        <form action=\"?mod=admin&file=users\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\">#Auto </td></tr>
<tr><td class=\"field_name\">Username</td><td class=\"field_object\"><input type=\"text\" name=\"username\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Password</td><td class=\"field_object\"><input type=\"password\" name=\"password\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Fullname</td><td class=\"field_object\"><input type=\"text\" name=\"fullname\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Address</td><td class=\"field_object\"><input type=\"text\" name=\"address\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">City</td><td class=\"field_object\"><input type=\"text\" name=\"city\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">State</td><td class=\"field_object\"><input type=\"text\" name=\"state\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Country</td><td class=\"field_object\"><input type=\"text\" name=\"country\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Date Join</td><td class=\"field_object\">
<input type=\"text\" name=\"date_join\" class=\"textbox\" rel=\"datepicker\"/> </td></tr>
<tr><td class=\"field_name\">Date Of Birth</td><td class=\"field_object\">
<input type=\"text\" name=\"date_of_birth\" class=\"textbox\" rel=\"datepicker\"/> </td></tr>
<tr><td class=\"field_name\">Group Id</td><td class=\"field_object\">
$select_groups </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function _edit(){
    global $db,$id;
    if (!empty($id)){
        $sql="select * from users where id=$id";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            extract($row);
             $select_groups=  select_groups($group_id);
            echo "<h2>Update User Profile</h2>
        <form action=\"?mod=admin&file=users\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\">
<input type=\"hidden\" name=\"id\" value=\"$id\"/> </td></tr>
<tr><td class=\"field_name\">Username</td><td class=\"field_object\">
<input type=\"text\" name=\"username\" class=\"textbox\" value=\"$username\"/> </td></tr>
<tr><td class=\"field_name\">Password</td><td class=\"field_object\">
<input type=\"password\" name=\"password\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Fullname</td><td class=\"field_object\">
<input type=\"text\" name=\"fullname\" class=\"textbox\" value=\"$fullname\"/> </td></tr>
<tr><td class=\"field_name\">Address</td><td class=\"field_object\">
<input type=\"text\" name=\"address\" class=\"textbox\" value=\"$address\"/> </td></tr>
<tr><td class=\"field_name\">City</td><td class=\"field_object\">
<input type=\"text\" name=\"city\" class=\"textbox\" value=\"$city\"/> </td></tr>
<tr><td class=\"field_name\">State</td><td class=\"field_object\">
<input type=\"text\" name=\"state\" class=\"textbox\" value=\"$state\"/> </td></tr>
<tr><td class=\"field_name\">Country</td><td class=\"field_object\">
<input type=\"text\" name=\"country\" class=\"textbox\" value=\"$country\"/> </td></tr>
<tr><td class=\"field_name\">Date Join</td><td class=\"field_object\">
<input type=\"text\" name=\"date_join\" class=\"datefield\" rel=\"datepicker\" value=\"$date_join\"/> </td></tr>
<tr><td class=\"field_name\">Date Of Birth</td><td class=\"field_object\">
<input type=\"text\" name=\"date_of_birth\" class=\"datefield\" rel=\"datepicker\" value=\"$date_of_birth\"/> </td></tr>
<tr><td class=\"field_name\">Group</td><td class=\"field_object\">
$select_groups</td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
        }
    }
}
function _add_process(){
    global $id,$username,$password,$fullname,$address,$city,$state,$country,$date_join,$date_of_birth,$group_id,$db;
    if (!empty($username)){
        $sql="insert into users values (null,'$username','$password','$fullname',
            '$address','$city','$state','$country','$date_join','$date_of_birth','$group_id')";
        $db->query($sql);
        refresh_to("?mod=admin&file=users", "Back to User List", 2);
    }
}
function _edit_process(){
    global $id,$username,$password,$fullname,$address,$city,$state,$country,$date_join,$date_of_birth,$group_id,$db;
     if (!empty($username)){
         if (empty($password)){
             $sql="update users set username='$username',fullname='$fullname',
            address='$address',city='$city',state='$state',country='$country',
                date_join='$date_join',date_of_birth='$date_of_birth',group_id='$group_id' where id=$id";
         } else {
             $sql="update users set username='$username',fullname='$fullname',password='$password'
            address='$address',city='$city',state='$state',country='$country',
                date_join='$date_join',date_of_birth='$date_of_birth',group_id='$group_id' where id=$id";
         }
        
        $db->query($sql);
        refresh_to("?mod=admin&file=users", "Back to User List", 2);
    }
}
function _delete(){
   global $db,$id;
    if (!empty($id)){
        $sql="delete from users";
        $db->query($sql);
        refresh_to("?mod=admin&file=users", "Back to Content Management", 1);
    }
}
function get_max_page(){
    global $db,$pagesize;
    $sql="select count(*) from users";
    $row=$db->fetch_row($sql);
    if (!empty($row)){
        return floor($row[0]/$pagesize)+1;
    } else return 0;
}
function _default(){
    global $db,$page,$pagesize,$mod,$file;
    if (empty($page)){
        $start=0;
        $page=0;
    } else {
        $start=$page*$pagesize;
    }
    $sql="select * from users limit $start,$pagesize";
    $result=$db->query($sql);
    $mp=  get_max_page();
     
    echo "<h2>User List</h2>";
    echo "<table width='100%' cellspacing=0 cellpadding=2>";
    echo "<tr><td>";
    for($i=0;$i<$mp;$i++){
        $pname=$i+1;
        echo "<a href=\"?mod=$mod&file=users&page=$i\">$pname</a> |";
    }
    echo "<a href=\"?mod=$mod&file=users&action=add\">Add New</a></td></tr>
    <tr><th>Id</th><th>Username</th><th>Password</th><th>Fullname</th><th>Address</th>
    <th>City</th><th>State</th><th>Country</th><th>Date Join</th><th>Date Of Birth</th><th>Group Id</th><th>Action</th></tr>";
    while ($row=$db->fetch_array($result)){
        extract($row);
        $password="**********";
        echo "<tr><td>$id</td><td>$username</td><td>$password</td>
            <td>$fullname</td><td>$address</td><td>$city</td><td>$state</td><td>$country</td><td>$date_join</td><td>$date_of_birth</td><td>$group_id</td><td><a href=\"?mod=$mod&file=$file&action=edit&id=$id\">
        <img class=\"icon_small\" src='img/edit.png' title='Edit'/></a>
        <a href=\"?mod=$mod&file=$file&action=delete&id=$id\"><img class=\"icon_small\" src='img/delete.png' title='Delete'/></a></td></tr>";
    }
    echo "<tr><td>";
    for($i=0;$i<$mp;$i++){
        $pname=$i+1;
        echo "<a href=\"?mod=$mod&file=users&page=$i\">$pname</a> |";
    }
    echo "</tr>";
    echo "</table>";
}
global $action;
if (!isset($is_login)){
    refresh_to("?mod=admin", "You Must be login first...", 2);
} else {
show_menus('header');
echo "<div class=\"sub_content\">";
switch($action){
    case 'add':
        _add();
        break;
    case 'add_process':
        _add_process();
        break;
    case 'edit':
        _edit();
        break;
    case 'edit_process':
        _edit_process();
        break;
    case 'delete':
        _delete();
        break;
    default :
        _default();
}
echo "</div>";
}
?>
